package com.thingcom.delinrobot.main.adddevice;

import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.thingcom.delinrobot.R;
import com.thingcom.delinrobot.base.BaseBackFragment;
import com.thingcom.delinrobot.utils.CommonDialog;
import com.thingcom.delinrobot.utils.WifiUtils;
import com.thingcom.delinrobot.view.SimpleToolbar;

import java.util.Objects;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * author : YuFeng
 * e-mail : 923462992@qq.com
 * date   : 2019/12/6
 */
public class AddDeviceSecondFragment extends BaseBackFragment {

    @BindView(R.id.toolbar_add_device_second)
    SimpleToolbar toolbarAddDeviceSecond;
    @BindView(R.id.btn_device_add_second)
    Button btnDeviceAddSecond;

    public static AddDeviceSecondFragment newInstance() {
        Bundle args = new Bundle();
        AddDeviceSecondFragment fragment = new AddDeviceSecondFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_back_device_add_second, container, false);
        unbinder = ButterKnife.bind(this, view);
        initToolbar(toolbarAddDeviceSecond);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !checkGPSIsOpen()) {
            CommonDialog fragment = CommonDialog.newInstance(getString(R.string.start_tip), getString(R.string.open_position));
            fragment.setCancelable(false);
            if (getFragmentManager() != null) {
                fragment.show(getFragmentManager(), "dialog");
            }
            fragment.setOnOkListener((() -> {
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(intent);
            }));
            fragment.setOnCancerListener((this::pop));
        }
    }

    //有些设备需要gps打开
    private boolean checkGPSIsOpen() {
        LocationManager locationManager = (LocationManager) Objects.requireNonNull(getActivity()).getSystemService(Context.LOCATION_SERVICE);
        if (locationManager == null)
            return false;
        return locationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER);
    }

    @OnClick(R.id.btn_device_add_second)
    public void onViewClicked() {
        if (WifiUtils.getInstance(getContext()).isWifiEnable()) {
            start(DeviceWifiBackFragment.newInstance());
        } else {
            WifiUtils.getInstance(getContext()).openWifi();
            start(DeviceWifiBackFragment.newInstance());
        }
    }
}
